// pages/register/register.js
var app = getApp();
var ea = require('../../utils/each.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    qiequ:false,
    src: '',
    width: 250,//宽度
    height: 250,//高度

    getguanxi: app.data.url + '/zhongxue/maoapi/getguanxi',
    guanxi: [],
    index: 0,
    datas: {
      
      name: '',   //孩子姓名
      relationship: '', //关系
      sex: '',         //性别
      birthday: '2016-09-01',    //生日
      touxiang: '',  //头像
      study: '',  //学习状况
      bodys: '',   //身体状况
    },

    date: '2016-09-01',
    gister: app.data.url + '/zhongxue/maoapi/chenge_student',
    upload: app.data.url + '/zhongxue/maoapi/getqiniutoken',
    jiajia: app.data.url +"/uploads/zhongxue/jiajia.png",
    items: [
      { name: "男", chceked: true },
      { name: "女" }
    ],
    array: ["母子", "母女", "父女", "父子", "其他"],
    arrays: ["6岁", "7岁", "8岁", "9岁", "10岁", "11岁", "12岁", "13岁", "14岁", "15岁", "16岁", "17岁", "18岁", "19岁", "20岁", "其他"],
  },

  //获取参数 
  bindinputs: function (e) {
    var value = e.detail.value;
    var key = e.currentTarget.dataset.key;
    var datas = this.data.datas;
    datas[key] = value;
    this.setData({ datas: datas });
  },
  // 获取性别
  radioChanges: function (e) {
    var es = e.detail.value;
    var dates = this.data.datas;
    dates["sex"] = es;
    this.setData({
      datas: dates
    })
  },
  //获取生日
  bindDateChange: function (e) {
    let that = this;
    var datas = that.data.datas;
    datas["birthday"] = e.detail.value;
    console.log(datas);
    that.setData({
      datas: datas,
      date: e.detail.value
    })
  },

  //获取亲属关系
  bindPickerChanges: function (e) {
    var value = e.detail.value;
    var guanxi = this.data.guanxi;
    var values = guanxi[value].id
    var datas = this.data.datas;
    datas["relationship"] = values;
    console.log(datas);
    this.setData({ datas: datas, index: value });
  },


  //获取头像的照片
  uploade: function () {
    var that = this;
    wx.chooseImage({
      count: 1,
      // sizeType: [],
      // sourceType: [],
      success: function(res) {
        that.setData({ qiequ:true});
        var src = res.tempFilePaths[0];
        that.cropper = that.selectComponent("#image-cropper");
        that.cropper.getImg = function(ress){
          console.log(ress);
        }
        that.setData({
          src: src,
        });
        wx.showLoading({
          title: '加载中'
        })
      },
      fail: function(res) {},
      complete: function(res) {},
    })
    // wx.chooseImage({
    //   count: 1, // 默认9
    //   sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
    //   sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
    //   success: res => {
    //     wx.showLoading({
    //       title: '上传中',
    //     })
        
    //     // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
    //     let tempFilePaths = res.tempFilePaths;
    //     let datas = that.data.datas;
    //     // that.setData({
    //     //   jiajia: tempFilePaths[0]
    //     // })
    //     console.log(tempFilePaths);
        // ea.upfile(tempFilePaths, function (filename) {
        //   var names = filename[0] +'?imageMogr2/auto-orient/thumbnail/300x/gravity/Center/crop/!300x300';
        //   var datas = that.data.datas;
        //   that.setData({
        //     jiajia: names
        //   })
        //   datas.touxiang = names;
        //   wx.hideLoading();
        // }, that.data.upload)

    //   }
    // })
  },




  //学习状况
  qis: function (e) {
    var vale = e.detail.value;
    var datas = this.data.datas;
    datas["study"] = vale;
    this.setData({
      datas: datas
    })

  },
  //身体状况
  qiss: function (e) {
    var vale = e.detail.value;
    var datas = this.data.datas;
    datas["bodys"] = vale;
    this.setData({
      datas: datas
    })
  },



  //注册
  register: function (e) {
    var that = this;
    var tss = that.data.datas;
    // console.log(tss);
    // return
    tss.token = wx.getStorageSync("user").token;
    // console.log(tss);
    // return
    wx.request({
      url: this.data.gister,
      data: tss,
      method: 'post',
      success: function (res) {
        var data = res.data;
        console.log(data);
        if(data.code==200){
          wx.showToast({
            title: '添加成功',
            duration: app.data.quanbushijian
          })
          setTimeout(function(){
            wx.navigateBack({
              delta: 1,
            })
          },1300)
        }else{
          wx.showToast({
            title: data.msg,
            icon: 'none',
            duration: app.data.quanbushijian
          })
        }
      }
    })

  },



  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var user = wx.getStorageSync('user');
    var that = this;
    wx.showLoading({
      title: '加载中',
    })
    wx.request({
      url: that.data.getguanxi,
      data: { token: user.token },
      method: "POST",
      success: function (res) {
        var data = res.data;
        that.setData({guanxi:data.data});
        var datas = that.data.datas;
        datas.relationship = data.data[0].id;
        that.setData({data})
      },
      complete: function () {
        wx.hideLoading();
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      path: '/pages/indexfirst/indexfirst',
    }
  },
  cropperload(e) {
    console.log("cropper初始化完成");
  },
  loadimage(e) {
    console.log("图片加载完成", e.detail);
    wx.hideLoading();
    //重置图片角度、缩放、位置
    this.cropper.imgReset();
  },
  clickcut(e) {
    // console.log(e.detail);
    //点击裁剪框阅览图片
    // console.log(e.detail.url);
    wx.showLoading({
      title: '上传中',
    })
    var that = this;
    ea.upfile([e.detail.url], function (filename) {
      var names = filename[0];
      var datas = that.data.datas;
      that.setData({
        jiajia: names,
        qiequ:false
      })
      datas.touxiang = names;
      wx.hideLoading();
    }, that.data.upload)
    // this.setData({ xinsrc: e.detail.url, status: false })

    // wx.previewImage({
    //   current: e.detail.url, // 当前显示图片的http链接
    //   urls: [e.detail.url] // 需要预览的图片http链接列表
    // })
  },
})